/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gitee.fdc.base.mapper.logicdelete;

import com.gitee.fdc.mapper.SbbsSqlHelper;
import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

/**
 *
 * @author qskj
 */
public class DeleteLogicProvider extends MapperTemplate {

    public DeleteLogicProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    public String deleteLogicByPrimaryKey(MappedStatement ms) {
        final Class<?> entityClass = getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
//        sql.append(SqlHelper.deleteFromTable(entityClass, tableName(entityClass)));

        sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass)));
        sql.append(SbbsSqlHelper.updateDeleteLogicColumn(entityClass, null, false, false,false));
        //增加 @Version 乐观锁支持
        sql.append(SqlHelper.wherePKColumns(entityClass, true));
        return sql.toString();
    }
                   
     public String unDeleteByPrimaryKey(MappedStatement ms) {
        final Class<?> entityClass = getEntityClass(ms);
        StringBuilder sql = new StringBuilder();

        EntityColumn deleteLogicColumn = SbbsSqlHelper.findDeleteLogicColumn(entityClass);

        if (deleteLogicColumn == null) {
            sql.append(SqlHelper.deleteFromTable(entityClass, tableName(entityClass)));
            //增加 @Version 乐观锁支持
            sql.append(SqlHelper.wherePKColumns(entityClass, true));
            return sql.toString();
        } else {
            //增加 @Version 乐观锁支持  
            sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass)));
            sql.append(SbbsSqlHelper.updateDeleteLogicColumn(entityClass, null, false, false,true));
            sql.append(SbbsSqlHelper.wherePKColumns(entityClass, true,true));
            return sql.toString();
        }
    }
}
